﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using BLL;
using System.Security.Cryptography.X509Certificates;
using System.Net.Security;
using System.Net;

public partial class PersonInfo_PersonList : BasePage
{
    PersonInfoBO pbo = new PersonInfoBO();
    IntegralChangeBO ibo = new IntegralChangeBO();
    //DataSet ds = null;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        { }
            if (GetQeuryString("para") == "GetList")
            {
                BindData();
            }
            //else if (GetQeuryString("para") == "delete")
            //{
            //    try
            //    {
            //        Response.Clear();
            //        string id = GetQeuryString("id");
            //        pbo.Delete(int.Parse(id));
            //        Response.Write("ok");
            //    }
            //    catch (Exception ex)
            //    {
            //        Response.Write("Error" + ex.Message);
            //    }
            //    finally
            //    {
            //        Response.End();
            //    }

            //}
            else if (GetQeuryString("para") == "excel")
            {
                try
                {
                    // Response.Clear();
                    string id = GetQeuryString("id");
                    DataSet ds = access.GetAllIntegralChangeExcel(" pid=" + Request.QueryString["id"].ToString());
                    CreateExcel(ds, "积分明细");
                    //Response.Write("ok");
                }
                catch (Exception ex)
                {
                    //Response.Write("Error" + ex.Message);
                }
                finally
                {
                    //Response.End();
                }

            }
            //else
            //{
            //    BindData();
            //}
      
    }
    

    private void BindData()
    { 
        string token = Token();
        Response.Clear();
        //string s = "{\"total\":\"239\",\"rows\":[  {\"code\":\"001\",\"name\":\"Name 1\",\"addr\":\"Address 11\",\"addr\":\"col4 data\"}   ]}  ";
        int PageNumber = Convert.ToInt32(Request.Params["page"] == null ? "1" : Request.Params["page"]) - 1;
        int pageSize = Convert.ToInt32(Request.Params["rows"] == null ? "10" : Request.Params["rows"]);
        string sort = " order by " + (Request.Params["sort"] == null ? "id" : Request.Params["sort"]) + " " + (Request.Params["order"] == null ? " desc " : Request.Params["order"]);
        string condition = " 1=1 ";
        string GiftNo = Request.Params["GiftNo"] == null ? "" : Request.Params["GiftNo"];
        string Name = Request.Params["Name"] == null ? "" : Request.Params["Name"];
        string JF = Request.Params["sj"] == null ? "" : Request.Params["sj"];
        string K1 = Request.Params["K1"] == null ? "" : Request.Params["K1"];
        string K2 = Request.Params["K2"] == null ? "" : Request.Params["K2"];
        string K3 = Request.Params["K3"] == null ? "" : Request.Params["K3"];
        string K4 = Request.Params["K4"] == null ? "" : Request.Params["K4"];
        string MD = Request.Params["md"] == null ? "" : Request.Params["md"];
        DropDownList1.SelectedValue = Request.Params["md"] == null ? "" : Request.Params["md"];
        DropDownList2.SelectedValue = Request.Params["K1"] == null ? "" : Request.Params["K1"];
        DropDownList3.SelectedValue = Request.Params["K2"] == null ? "" : Request.Params["K2"];
        DropDownList4.SelectedValue = Request.Params["K3"] == null ? "" : Request.Params["K3"];
        DropDownList5.SelectedValue = Request.Params["K4"] == null ? "" : Request.Params["K4"];
         
        //txtJT.Text = Request.Params["jt"] == null ? "" : Request.Params["jt"];
        //txtMD.Text = Request.Params["md"] == null ? "" : Request.Params["md"];
        if (GiftNo != "")
            condition += " and PersonNo LIKE '%" + GiftNo + "%' ";
        if (Name != "")
            condition += " and a.Name LIKE '%" + Name + "%' ";
        if (JF != "")
            condition += " and a.Phone LIKE '%" + JF + "%' ";
        if(K1!=""&&K2!="")
        if (K1 != "0"&&K2=="0")
            condition += " and DATEPART(MONTH,A.BIRTHDAY)=" + K1 + " ";
        else if(K1=="0"&&K2!="0")
            condition += " and DATEPART(day,A.BIRTHDAY)=" + K2 + " ";
        else if (K1 != "0" && K2 != "0")
        {
            double day = int.Parse(K1) + int.Parse(K2) / 100.0;
            condition += " and (DATEPART(MONTH,BIRTHDAY)+(DATEPART(day,BIRTHDAY)/100.0))>="+day;
        }

        if (K3 != "" && K4 != "")
            if (K3 != "0" && K4 == "0")
                condition += " and DATEPART(MONTH,A.BIRTHDAY)=" + K3 + " ";
            else if (K3 == "0" && K4 != "0")
                condition += " and DATEPART(day,A.BIRTHDAY)=" + K4 + " ";
            else if (K3 != "0" && K4 != "0")
            {
                double day = int.Parse(K3) + int.Parse(K4) / 100.0;
                condition += " and (DATEPART(MONTH,BIRTHDAY)+(DATEPART(day,BIRTHDAY)/100.0))<=" + day;
            }
        //if (K2 != "0")
        //    condition += " and a.BIRTHDAY<='" + K2 + "' ";
        if (MD != ""&&MD!="全部")
            condition += " and a.MEM_LEVEL='" + MD + "' ";
        //if (txtJT.Text != "")
        //    condition += "  AND JT='" + txtJT.Text + " '";
        //if (txtMD.Text != "")
        //    condition += " AND Store='" + txtMD.Text + "' ";
        DataSet ds= access.GetAllPerson(sort, pageSize, PageNumber, condition);
        ds.Tables[1].Columns.Add(new DataColumn("SR"));
        ds.Tables[1].Columns.Add(new DataColumn("XB"));
        ds.Tables[1].Columns.Add(new DataColumn("ZC"));
        foreach (DataRow dr in ds.Tables[1].Rows)
        {
            dr["SR"] = DateTime.Parse(dr["BIRTHDAY"].ToString()==""?"1900-01-01":dr["BIRTHDAY"].ToString()).ToString("yyyy-MM-dd");
            dr["XB"] = dr["Sex"].ToString() == "True" ? "男" : "女";
            dr["ZC"] = DateTime.Parse(dr["WX_DATE"].ToString() == "" ? "1900-01-01" : dr["WX_DATE"].ToString()).ToString("yyyy-MM-dd");
                   
        }
        string re = ToJson.DataTableToJson(ds.Tables[0].Rows[0][0].ToString(), "rows", ds.Tables[1]);
        Response.Write(re);
        Response.End();
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        BindData();
    }

    private bool ValidateServerCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
    {
        return true;
    }


    protected void Button1_Click(object sender, EventArgs e)
    {
        string token = Token();

        string condition = " 1=1 ";
        string GiftNo = txtGiftNo.Text;
        string Name = txtName.Text;
        string JF = txtJF.Text;
        //string K1 = txtK1.Text;
        //string K2 = txtK2.Text;
        if (GiftNo != "")
            condition += " and PersonNo LIKE '%" + GiftNo + "%' ";
        if (Name != "")
            condition += " and Name LIKE '%" + Name + "%' ";
        if (JF != "")
            condition += " and Phone LIKE '%" + Name + "%' ";
        //if (K1 != "")
        //    condition += " and SumIntegral>=" + K1 + " ";
        //if (K2 != "")
        //    condition += " and SumIntegral<=" + K2 + " ";
        //if (txtJT.Text != "")
        //    condition += "  AND JT='" + txtJT.Text + " '";
        //if (txtMD.Text != "")
        //    condition += " AND Store='" + txtMD.Text + "' ";
        DataSet ds = access.GetPersonExcel(condition);

        //foreach (DataRow dr in ds.Tables[0].Rows)
        //{
        //    ServicePointManager.ServerCertificateValidationCallback = ValidateServerCertificate;
        //    string url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=" + token + "&openid=" + dr["微信id"].ToString();
        //    string b = PostRequest(url);
        //    string[] a = b.Split('\"');
        //    if (a.Length == 35)
        //    {
        //        if (a[9].ToString() != "")
        //        {
        //            dr["微信名称"] = a[9].ToString();

        //        }
        //    }
        //}

            CreateExcel(ds, "会员信息");
       
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        string condition = " 1=1 ";
        string GiftNo = txtGiftNo.Text;
        string Name = txtName.Text;
        string JF = txtJF.Text;
        //string K1 = txtK1.Text;
        //string K2 = txtK2.Text;
        if (GiftNo != "")
            condition += " and PersonNo LIKE '%" + GiftNo + "%' ";
        if (Name != "")
            condition += " and Name LIKE '%" + Name + "%' ";
        if (JF != "")
            condition += " and Phone LIKE '%" + Name + "%' ";
        //if (K1 != "")
        //    condition += " and SumIntegral>=" + K1 + " ";
        //if (K2 != "")
        //    condition += " and SumIntegral<=" + K2 + " ";
        //if (txtJT.Text != "")
        //    condition += "  AND JT='" + txtJT.Text + " '";
        //if (txtMD.Text != "")
        //    condition += " AND Store='" + txtMD.Text + "' ";

        DataSet ds = access.GetPersonAddressExcel(condition);
        CreateExcel(ds, "联系信息");
    }


    protected void Button3_Click(object sender, EventArgs e)
    {
        string token = Token();
        DataSet ds = pbo.GetList("");
        if (ds != null)
        {        
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                          
                ServicePointManager.ServerCertificateValidationCallback = ValidateServerCertificate;
                string url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=" + token + "&openid=" + ds.Tables[0].Rows[i]["FromUserName"].ToString();
                string b = PostRequest(url);
                string[] a = b.Split('\"');
                if (a.Length == 35)
                {
                    if (a[9].ToString() != "")
                    {
                        string WXName = a[9].ToString();
                        pbo.Update(int.Parse(ds.Tables[0].Rows[i]["ID"].ToString()), WXName);

                    }
                }
           
            }


        }

    }

}